<?php
	require_once '../../core/Debug.php'; //System Debug...
	require_once '../../core/config.php';//System Config...
	require_once '../../core/Init.php';  //System Init...

    if(User::have_permit(ADMIN_ITEM) && (int)Url::get('user_id',0) && (int)Url::get('user_id',0)!=User::id())
    {
        $user_id	= (int)Url::get('user_id',0);
        $user_name	= '';
        $user		= User::getUser($user_id);
        if($user)
        $user_name	= $user['user_name'];
    }
    else{
        $user_id	= (int)Url::get('cur_user_id',0);
        $user_name	= '';
        $user		= User::getUser($user_id);
        
        if($user)
        $user_name	= $user['user_name'];
        
        User::$current->data=$user;
        User::$current->groups = User::get_groups($user['gids']);
        
        $_SESSION['user_id'] = $user_id;
        $_SESSION['user_name']=$user_name;
    }
    
    $arrRet['error'] 		= 'not_uploaded';
    $arrRet['id'] 			= 0;
    $arrRet['img_server'] 	= -1;
    $arrRet['image_url']	= '';
    $countImage = $_REQUEST['countImage'];

    if(User::is_login()){
        if(User::have_permit(ADMIN_ITEM))
        {
            $upload_path= AZLib::folderUpload();

            if($countImage <= 100)
            {
                if(AZLib::ftp_check_dir($upload_path,true,IMAGE_SERVER_NO))//Check dir for upload
                {
                    if(isset($_FILES['Filedata']) && $_FILES['Filedata']['name']){
                        $file_name	=$_FILES['Filedata']['name'];
                        $sourceName 	=$_FILES['Filedata']['tmp_name'];
                        $file_ext	=AZLib::getExtension($file_name);

                        if(in_array($file_ext,array('.jpg','.jpeg','.gif','.png'))){
                            if(!$_FILES['Filedata']['error'])
                            {
                                list($imagewidth, $imageheight, $imageType) = getimagesize($_FILES['Filedata']['tmp_name']);
                                if($imagewidth && $_FILES['Filedata']['size']<=Item::MAX_UPLOAD_SIZE ){

                                    $img_url   = $upload_path.date("YmdHis",TIME_NOW) . '_'.substr(AZLib::make_safe_name(basename(strtolower($file_name),$file_ext)),0,36).$file_ext;

                                    if(AZLib::ftp_image_put_file($img_url,$sourceName,IMAGE_SERVER_NO)){
                                        $image_id = DB::insert('item_image',array(
                                                                                  'des'					=>'',
                                                                                  'item_id'				=>0,
                                                                                  'title'				=>'',
                                                                                  'position'			=>0,
                                                                                   'img_server'	=>IMAGE_SERVER_NO,
                                                                                  'img_url'	=>$img_url,
                                                                                  'root_id'				=>0,
                                                                                  'time'				=>TIME_NOW,
                                                                                  'user_id'				=>$user_id,
                                                                                  'user_name'			=>$user_name
                                                                                  ));
                                        if($image_id){
                                            $arrRet['id'] 			= $image_id;
                                            $arrRet['img_server'] 	= IMAGE_SERVER_NO;
                                            $arrRet['image_url']	= $img_url;
                                            $arrRet['error'] 		= 'success';
                                        }
                                    }
                                }
                                else{
                                    $arrRet['error'] = 'over_max_size';//Sai Ä‘á»‹nh dáº¡ng file hoáº·c upload dung lÆ°á»£ng quÃ¡ lá»›n
                                }
                            }
                            elseif($_FILES['Filedata']['error'] == 1)
                            {
                                $json['error'] = 'over_max_size';
                            }
                        }
                        else{
                            $arrRet['error'] = 'ext_invalid';//Sai Ä‘á»‹nh dáº¡ng file
                        }
                    }
                    else
                    {
                        $json['error'] = 'no_file';
                    }
                }
            }
            else{
                $arrRet['error'] = 'not_have_permission';
            }
        }
        else
        {
            $arrRet['error'] = 'not_have_permission';
        }
    }
    else{
        $arrRet['error'] = 'not_login';
    }

    echo implode(',',$arrRet);
    exit();
?>